﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace GFramework
{
    public static class CompositeFactory
    {
        public static Leaf Create(Type type)
        {
            Leaf leaf = (Leaf)ReferencePool.Acquire(type);
            CompositeEventHelper.Awake(leaf);
            return leaf;
        }

        public static TLeaf Create<TLeaf>() where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            CompositeEventHelper.Awake(leaf);
            return leaf;
        }

        public static TLeaf Create<TLeaf, T>(T obj) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            CompositeEventHelper.Awake(leaf, obj);
            return leaf;
        }

        public static TLeaf Create<TLeaf, T1, T2>(T1 obj1, T2 obj2) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            CompositeEventHelper.Awake(leaf, obj1, obj2);
            return leaf;
        }

        public static TLeaf Create<TLeaf, T1, T2, T3>(T1 obj1, T2 obj2, T3 obj3) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            CompositeEventHelper.Awake(leaf, obj1, obj2, obj3);
            return leaf;
        }

        public static Leaf CreateWithParent(Type type, Composite parent)
        {
            Leaf leaf = (Leaf)ReferencePool.Acquire(type);
            leaf.Parent = parent;
            CompositeEventHelper.Awake(leaf);
            return leaf;
        }

        public static TLeaf CreateWithParent<TLeaf>(Composite parent) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            leaf.Parent = parent;
            CompositeEventHelper.Awake(leaf);
            return leaf;
        }

        public static TLeaf CreateWithParent<TLeaf, T>(Composite parent, T obj) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            leaf.Parent = parent;
            CompositeEventHelper.Awake(leaf, obj);
            return leaf;
        }

        public static TLeaf CreateWithParent<TLeaf, T1, T2>(Composite parent, T1 obj1, T2 obj2) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            leaf.Parent = parent;
            CompositeEventHelper.Awake(leaf, obj1, obj2);
            return leaf;
        }

        public static TLeaf CreateWithParent<TLeaf, T1, T2, T3>(Composite parent, T1 obj1, T2 obj2, T3 obj3) where TLeaf : Leaf, new()
        {
            TLeaf leaf = ReferencePool.Acquire<TLeaf>();
            leaf.Parent = parent;
            CompositeEventHelper.Awake(leaf, obj1, obj2, obj3);
            return leaf;
        }

        public static void Destory(Leaf leaf)
        {
            CompositeEventHelper.Shutdown(leaf);
            ReferencePool.Release(leaf);
        }
    }
}
